package com.fafian.gdc.app.android;


import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class RemoteControl extends Activity {

	private static GDCServerPreferences preferences;
	private static ButtonClickListener bcl;
	public static final String SHARED_PREFERENCES_NAME=
			"com.fafian.gdc.app.android.RemoteControl";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_remote_control);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		readPreferencesFromStorage();
		Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		bcl = new ButtonClickListener(preferences, v);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.remote_control, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			Intent intent = new Intent(getBaseContext(), Configuration.class);
			savePreferencesToStorage();
			startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_remote_control,
					container, false);
			
			((Button) rootView.findViewById(R.id.button1)).setOnClickListener(bcl);
			((Button) rootView.findViewById(R.id.button2)).setOnClickListener(bcl);

			return rootView;
		}
	}
	
	protected void onStop(){
		super.onStop();
		savePreferencesToStorage();
	}
	
	protected void onResume(){
		super.onResume();
		readPreferencesFromStorage();
		bcl.setPreferences(preferences);
	}
	
	private void readPreferencesFromStorage(){
		SharedPreferences settings = getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
		preferences = new GDCServerPreferences(
				settings.getString(GDCServerPreferences.APIKEY_NAME, 
						"IWb2aZkpk2dUZbb7GQj5DPSpy4fniNMv"),
				settings.getString(GDCServerPreferences.SERVER_NAME,
						"192.168.1.111"),
				settings.getInt(GDCServerPreferences.PORT_NUMBER_NAME, 
						44344)
		);
	}
	
	private void savePreferencesToStorage(){
		SharedPreferences settings = getSharedPreferences(SHARED_PREFERENCES_NAME, 0);
		Editor edit = settings.edit();
		edit.putString(GDCServerPreferences.APIKEY_NAME, 
				preferences.getApiKey());
		edit.putString(GDCServerPreferences.SERVER_NAME, 
				preferences.getServer());
		edit.putInt(GDCServerPreferences.PORT_NUMBER_NAME, 
				preferences.getPortNumber());
		edit.commit();
		
	}	
	

}
